package com.ouc.pampas;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FrontConfig extends BaseConfig {
	private static final long serialVersionUID = 7453802881470194536L;
	public static final String HREF_BASE = "base";
	public static final String HREF_MAIN = "main";
	public static final String HREF_LOGIN = "login";
	private Map<String, Component> components;

	public String toString() {
		return "FrontConfig(components=" + getComponents() + ", componentCategoryListMap="
				+ getComponentCategoryListMap() + ", mappings=" + getMappings() + ", auths=" + getAuths() + ", render="
				+ getRender() + ", hrefs=" + getHrefs() + ")";
	}

	public Map<String, Component> getComponents() {
		return this.components;
	}

	public void setComponents(Map<String, Component> components) {
		this.components = components;
	}

	public Map<Component.ComponentCategory, List<Component>> getComponentCategoryListMap() {
		return this.componentCategoryListMap;
	}

	private Map<Component.ComponentCategory, List<Component>> componentCategoryListMap = Maps.newHashMap();
	private Set<Mapping> mappings;
	private Auths auths;
	private Render render;

	public void setComponentCategoryListMap(
			Map<Component.ComponentCategory, List<Component>> componentCategoryListMap) {
		this.componentCategoryListMap = componentCategoryListMap;
	}

	public Set<Mapping> getMappings() {
		return this.mappings;
	}

	public void setMappings(Set<Mapping> mappings) {
		this.mappings = mappings;
	}

	public Auths getAuths() {
		return this.auths;
	}

	public void setAuths(Auths auths) {
		this.auths = auths;
	}

	public Render getRender() {
		return this.render;
	}

	public void setRender(Render render) {
		this.render = render;
	}

	public Map<String, String> getHrefs() {
		return this.hrefs;
	}

	private Map<String, String> hrefs = Maps.newHashMap();

	public void setHrefs(Map<String, String> hrefs) {
		this.hrefs = hrefs;
	}

	public void merge(BaseConfig config) {
		if (!(config instanceof FrontConfig)) {
			throw new IllegalArgumentException("merged config is not FrontConfig");
		}
		FrontConfig mergedConfig = (FrontConfig) config;
		if ((mergedConfig.getComponents() != null) && (!mergedConfig.getComponents().isEmpty())) {
			if (this.components == null) {
				this.components = Maps.newHashMap();
			}
			this.components.putAll(mergedConfig.getComponents());
		}
		if ((mergedConfig.getMappings() != null) && (!mergedConfig.getMappings().isEmpty())) {
			if (this.mappings == null) {
				this.mappings = Sets.newHashSet();
			}
			this.mappings.addAll(mergedConfig.getMappings());
		}
		if (mergedConfig.getAuths() != null) {
			if (this.auths == null) {
				this.auths = new Auths();
			}
			this.auths.merge(mergedConfig.getAuths());
		}
		if (mergedConfig.getRender() != null) {
			if (this.render == null) {
				this.render = new Render();
			}
			this.render.merge(mergedConfig.getRender());
		}
		if ((mergedConfig.getHrefs() != null) && (!mergedConfig.getHrefs().isEmpty())) {
			if (this.hrefs == null) {
				this.hrefs = Maps.newHashMap();
			}
			this.hrefs.putAll(mergedConfig.getHrefs());
		}
	}

	public Map<String, String> getCurrentHrefs(String currentDomain) {
		Map<String, String> currentHrefs = Maps.newHashMap(this.hrefs);
		if (currentHrefs.get("base") == null) {
			currentHrefs.put("base", currentDomain);
		}
		if (currentHrefs.get("main") == null) {
			currentHrefs.put("main", "http://" + currentDomain);
		}
		if (currentHrefs.get("login") == null) {
			currentHrefs.put("login", (String) currentHrefs.get("main") + "/login");
		}
		return currentHrefs;
	}
}
